import datetime
from flask import Blueprint, request
from conn_mysql import ConnectMysql


app_set_password = Blueprint('app_set_password', 'api')

@app_set_password.route('/app_set_password', methods=['POST'])
def set_password():
    phone = request.form.get('phone')
    one_password = request.form.get('one_password')
    two_password = request.form.get('two_password')
    
    if (not all([one_password, two_password, phone])) or (one_password != two_password):

        return {
            'status': -1,
            'errorCode': 51,
            'errorMsg': '请检查你设置的密码',
            'resultBody': None,
        }
    
    with ConnectMysql() as c:
        sql = "INSERT INTO user_table(user, pass_word) VALUES (%s, %s)"
        sql_ = "SELECT * FROM user_table WHERE user=%s"

        result = c.select_one(sql_, (phone,))

        if result:

            sql__ = "UPDATE user_table SET pass_word=%s WHERE user=%s"

            c.update_info(sql__, (one_password, phone))
            
            Id = result['id']

        else:

            ID = c.insert_info(sql, (phone, one_password))

    return {

        'status': 1,
        'errorCode': None,
        'errorMsg': None,
        'resultBody': "密码设置成功",

    }


